import user from 'src/api/user';
import meEndpoint from 'src/api/meEndpoint';
import { setIsLogin, resetAuth } from 'src/redux/authSlice';
import { resetRebate } from 'src/redux/rebateSlice';
import { dispatch } from 'src/redux/store';
import { finishWaiting, startWaiting } from 'src/redux/uiSlice';
import { setAccountInfo } from 'src/redux/authSlice';

type PostAuthRequest = any

export const signup = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.signup(data);
        return data.res = res.data
    } finally {
        dispatch(finishWaiting());
    }
};

export const signin = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.signin(data);
        if (!res?.data?.success) {
            if (res.data.message == "待验证") {
                return data.validate = "unverified";
            } else if (res.data.message == "二次验证码") {
                return data.secondValidate = "otp";
            } else {
                return data.errorMessage = res.data.message;
            }
        }
        localStorage.setItem('token', res.data.token);
        localStorage.setItem('uMail', res.data.uMail);
        dispatch(setIsLogin());
        return res.data.message !== "待开启" ? 'has-otp' : 'no-otp';
    } finally {
        dispatch(finishWaiting());
    }
};


export const signout = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        await user.signout(data);
        localStorage.removeItem('token');
        localStorage.removeItem('uMail');
        dispatch(resetRebate());
        dispatch(resetAuth());
    } finally {
        dispatch(finishWaiting());
    }

};


export const emailResend = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        await user.emailResend(data);
    } finally {
        dispatch(finishWaiting());
    }
};

export const emailVerify = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.emailVerify(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};

export const editName = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.editName(data);
        const userData = await meEndpoint.postMe(data)
        dispatch(setAccountInfo(userData?.data?.list[0]));
        return data.status = {
            success: res.data.success,
            message: res.data.message,
        };
    } finally {
        dispatch(finishWaiting());
    }
};

export const editPwd = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.editPwd(data);
        return data.status = {
            success: res.data.success,
            message: res.data.message,
        };
    } finally {
        dispatch(finishWaiting());
    }
};

export const getGoogleVerify = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.getGoogleVerify(data);
        if (res.data.success) {
            return data.data = res.data
        }

    } finally {
        dispatch(finishWaiting());
    }
};

export const GoogleVerify = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.GoogleVerify(data);
        if (res.data.success) {
            const userData = await meEndpoint.postMe(data)
            dispatch(setAccountInfo(userData?.data?.list[0]));
        }
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const UserGoogleYz = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.UserGoogleYz(data);
        if (res.data.success) {
            localStorage.setItem('token', res.data.token);
            localStorage.setItem('uMail', res.data.uMail);
            dispatch(setIsLogin());
        }
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};

export const getResetPwd = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.getResetPwd(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
// 读取银行列表getMyBank用户ID[guid]用户授权后的token值
export const getMyBank = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.getMyBank(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const addMyBank = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.addMyBank(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const delMyBank = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await user.delMyBank(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};


export const getUserInfo = async (data: PostAuthRequest) => {
    try {
        // dispatch(startWaiting());
        const res = await meEndpoint.postMe(data);
        return data.data = res.data
    } finally {
        // dispatch(finishWaiting());
    }
}; 